package com.example.mqtt.base;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.MqttMessage;

@Slf4j
public abstract class MqttReceiver implements IMqttMessageListener {

    /**
     * 定义要订阅的主题枚举
     */
    public abstract MqttSubTopic subTopic();

    /**
     * 消息接收
     */
    public abstract void receiver(String topic, MqttMessage message) throws Exception;

    @Override
    public void messageArrived(String topic, MqttMessage message) {
        try {
            receiver(topic, message);
        } catch (Throwable e) {
            log.error(String.format("主题: %s, 处理异常", topic), e);
        }
    }
}
